package com.augurit.gzcgwsp.ywcmn.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.BaseAction;
import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.ywcmn.convert.YwComSjcllistConvertor;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjclService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjcllistOtherService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjcllistService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjclForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjcllistForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjcllistOtherForm;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "yw-com-sjcllist.action", type = "redirect"),
    @Result(name = "YW_LIST", location = "/gzcgwsp/ywcmn/ywinc/sjclList.jsp"),
    @Result(name = "sjlist", location = "/gzcgwsp/ywcmn/ywinc/sjlist.jsp"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/gzcgwsp/ywcmn/YwComSjcllist/YwComSjcllist.jsp"),
	@Result(name = CrudActionSupport.INPUT, location = "/gzcgwsp/ywcmn/YwComSjcllist/YwComSjcllist-input.jsp")
})
public class YwComSjcllistAction extends CrudActionSupport<YwComSjcllistForm>{

	@Autowired
	private IYwComSjcllistService ywComSjcllistService;
	@Autowired
	private IYwComSjcllistOtherService ywComSjcllistOtherService;
    @Autowired
    private IYwComSjclService ywComSjclService;
	
	@Autowired
    private IWfBusTemplateService wfBusTemplateService;

	private YwComSjcllistForm form;
	private Long id;
	private Long[] checkedIds;
	private Page<YwComSjcllistForm> page = new Page<YwComSjcllistForm>(10);

	@Override
	public String list() throws Exception {
	    List<WfBusTemplateForm> tplList = wfBusTemplateService.getAllActive();
	    BaseAction.getRequest().setAttribute("tplList", tplList);
	    
		ywComSjcllistService.search(page, form);
		return SUCCESS;
	}

    public String ywList() throws Exception {
    	getRequest().setAttribute("CurrUserName", this.getLoginUser().getUser().getUserName());
        //这里获取收件材料是否可以修改 
        //参数由input页面传来因为在input的onload页面传入所以如果是不可编辑的话会加载两次
        String isUpdate=getRequest().getParameter("isUpdate");
        List<YwComSjcllistForm> sjclList = ywComSjcllistService.getList(form);
        YwComSjclForm fkForm = new YwComSjclForm();
        fkForm.setFkid(form.getQyid());
        fkForm.setAjid(form.getAjid());
        fkForm.setTplcode(form.getTplcode());
        List<YwComSjclForm> sjclTpl = ywComSjclService.getList(fkForm);
        
        for (YwComSjcllistForm listForm : sjclList) {
            boolean hasExists = false;
            for (YwComSjclForm tplForm : sjclTpl) {
                if (tplForm.getTplid() != null && listForm.getId().longValue() == tplForm.getTplid().longValue()) {
                    if (StringUtils.isNotBlank(tplForm.getFilepath())) {
                    	listForm.setSfdz("0");
                        listForm.setState("1");
                        listForm.setHasfile("1");
                        listForm.setFilename(tplForm.getFilename());
                        listForm.setFilepath(tplForm.getFilepath());
                        listForm.setFiletype(tplForm.getFiletype());
                    } else {
                        listForm.setState("0");
                        listForm.setHasfile("0");
                    }
                    if (tplForm.getYjs() != null) {
                        listForm.setYjs(Long.valueOf(tplForm.getYjs()));
                    }
                    if (tplForm.getFyjs() != null) {
                        listForm.setFyjs(Long.valueOf(tplForm.getFyjs()));
                    }
                    listForm.setClid(tplForm.getId());
                    if ("1".equals(listForm.getSfdz())) {
                        listForm.setDisabled("1");
                    } else {
                        listForm.setDisabled("0");
                        if (tplForm.getSfdz() == null) {
                            listForm.setSfdz("0");
                        } else {
                            listForm.setSfdz(tplForm.getSfdz());
                        }
                    }
                    hasExists = true;
                }
            }
            if (!hasExists) {
                listForm.setSfdz("1");
                listForm.setHasfile("0");
                listForm.setState("1");
                if ("1".equals(listForm.getSfdz())) {
                    listForm.setDisabled("1");
                } else {
                    listForm.setDisabled("0");
                }
            }
        }
        
        List<YwComSjcllistForm> sjclOtherList = ywOtherList();
        if(sjclOtherList != null && sjclOtherList.size()>0){
        	for(int i =0;i<sjclOtherList.size();i++){
            	sjclList.add(sjclOtherList.get(i));
            }
        }        
       
        super.getRequest().setAttribute("sjclList", sjclList);
        getRequest().setAttribute("isUpdate",isUpdate);
        return "YW_LIST";
    }
    
    public List<YwComSjcllistForm> ywOtherList() throws Exception {
        //这里获取收件材料是否可以修改 
        //参数由input页面传来因为在input的onload页面传入所以如果是不可编辑的话会加载两次
        String isUpdate=getRequest().getParameter("isUpdate");
        List<YwComSjcllistOtherForm> sjclList = ywComSjcllistOtherService.getList(form);
        YwComSjclForm fkForm = new YwComSjclForm();
        fkForm.setFkid(form.getQyid());
        fkForm.setAjid(form.getAjid());
        fkForm.setTplcode(form.getTplcode());
        List<YwComSjclForm> sjclTpl = ywComSjclService.getOtherList(fkForm);
        
        for (YwComSjcllistOtherForm listForm : sjclList) {
            boolean hasExists = false;
            for (YwComSjclForm tplForm : sjclTpl) {
                if (tplForm.getTplid() != null && listForm.getId().longValue() == tplForm.getTplid().longValue()) {
                    if (StringUtils.isNotBlank(tplForm.getFilepath())) {
                        listForm.setState("1");
                        listForm.setHasfile("1");
                        listForm.setFilename(tplForm.getFilename());
                        listForm.setFilepath(tplForm.getFilepath());
                        listForm.setFiletype(tplForm.getFiletype());
                    } else {
                        listForm.setState("0");
                        listForm.setHasfile("0");
                    }
                    if (tplForm.getYjs() != null) {
                        listForm.setYjs(Long.valueOf(tplForm.getYjs()));
                    }
                    if (tplForm.getFyjs() != null) {
                        listForm.setFyjs(Long.valueOf(tplForm.getFyjs()));
                    }
                    listForm.setClid(tplForm.getId());
                    if ("1".equals(listForm.getSfdz())) {
                        listForm.setDisabled("1");
                    } else {
                        listForm.setDisabled("0");
                        if (tplForm.getSfdz() == null) {
                            listForm.setSfdz("0");
                        } else {
                            listForm.setSfdz(tplForm.getSfdz());
                        }
                    }
                    hasExists = true;
                }
            }
            if (!hasExists) {
                listForm.setSfdz("1");
                listForm.setHasfile("0");
                listForm.setState("1");
                if ("1".equals(listForm.getSfdz())) {
                    listForm.setDisabled("1");
                } else {
                    listForm.setDisabled("0");
                }
            }
        }
        return YwComSjcllistConvertor.convertOtherSjcllistFormListToSjcllistFormList(sjclList);
    }
    
    /**
     * 获取所有的收件材料模版
     * @return
     * @throws Exception
     */
    public String  getSjclList() throws Exception {
    	String type=getRequest().getParameter("type");//分为不同的业务
    	String ajid=getRequest().getParameter("ajid");
    	String gclb=getRequest().getParameter("gclb");
    	String templateCode=getRequest().getParameter("templateCode");
    	if(StringUtils.isNotBlank(ajid)){
    		form.setAjid(Long.valueOf(ajid));
    	}
    	if(StringUtils.isNotBlank(gclb)){
    		form.setBelongType(gclb);
    	}
    
    	form.setTplcode(templateCode);
    	List<YwComSjcllistForm> list=ywComSjcllistService.getSjList(form,type);
    	getRequest().setAttribute("sjlist", list);
    	return "sjlist";
    }
    
    
    
    
    

	/**
	 * �������޸�
	 */
	@Override
	public String input() throws Exception {
        List<WfBusTemplateForm> tplList = wfBusTemplateService.getAllActive();
        BaseAction.getRequest().setAttribute("tplList", tplList);
		return INPUT;
	}

	/**
	 * �������������޸ĵ�Form����
	 */
	@Override
	public String save() throws Exception {
		ywComSjcllistService.save(form);
		return RELOAD;
	}

	/**
	 * ɾ��һ����¼
	 */
	@Override
	public String delete() throws Exception {
		ywComSjcllistService.delete(id);
		return RELOAD;
	}
	
	/**
	 * ɾ�������¼
	 */
	@Override
	public String deleteMore() throws Exception{
		ywComSjcllistService.delete(checkedIds);
		return RELOAD;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			form = ywComSjcllistService.get(id);
		}
		else
			form = new YwComSjcllistForm();
	}
	
	public YwComSjcllistForm getModel() {
		return form;
	}
	
	// ����getter��setter����
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(Long[] checkedIds) {
		this.checkedIds = checkedIds;
	}
	
	public Page<YwComSjcllistForm> getPage() {
		return page;
	}


	public void setPage(Page<YwComSjcllistForm> page) {
		this.page = page;
	}
}
